﻿Module Module1

    Sub Main()
        Console.WriteLine("Welcome to the Startup Manager Uninstaller")
        Console.WriteLine("Please press any key to continue or close this window to exit")
        Console.ReadLine()

        Try
            'Stop Startup Manager Service

            Try
                Console.WriteLine("Waiting for Startup Manager Service to stop...")
                Dim sms As Process = Process.GetProcessesByName("Startup Manager Service")(0)
                sms.Kill()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try

            'Replace all users' Registry
            Dim names() As String = My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run\Managed Startup").GetValueNames()
            Try
                For i As Integer = 0 To My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run\Managed Startup").ValueCount - 1
                    Dim keyvalue As String
                    keyvalue = My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run\Managed Startup").GetValue(names(i))
                    My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).SetValue(names(i), keyvalue)
                    My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run\Managed Startup", True).DeleteValue(names(i))
                Next
                My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).DeleteSubKey("Managed Startup")
                Try
                    My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue("Startup Manager")
                Catch
                End Try
                My.Computer.Registry.LocalMachine.OpenSubKey("Software", True).DeleteSubKey("Startup Manager")
            Catch
                Console.WriteLine("Please run this program as an administrator")
                Console.ReadLine()
                Exit Sub
            End Try

                'Replace individual users' registry
                Dim users() As String = My.Computer.Registry.Users.GetSubKeyNames
                For Each user As String In users
                    Try
                        Dim names2() As String = My.Computer.Registry.Users.OpenSubKey(user & "\Software\Microsoft\Windows\CurrentVersion\Run\Managed Startup").GetValueNames()
                        For i As Integer = 0 To My.Computer.Registry.Users.OpenSubKey(user & "\Software\Microsoft\Windows\CurrentVersion\Run\Managed Startup").ValueCount - 1
                            Dim keyvalue As String
                            keyvalue = My.Computer.Registry.Users.OpenSubKey(user & "\Software\Microsoft\Windows\CurrentVersion\Run\Managed Startup").GetValue(names2(i))
                            My.Computer.Registry.Users.OpenSubKey(user & "\Software\Microsoft\Windows\CurrentVersion\Run", True).SetValue(names2(i), keyvalue)
                            My.Computer.Registry.Users.OpenSubKey(user & "\Software\Microsoft\Windows\CurrentVersion\Run\Managed Startup", True).DeleteValue(names2(i))
                        Next
                        My.Computer.Registry.Users.OpenSubKey(user & "\Software\Microsoft\Windows\CurrentVersion\Run\", True).DeleteSubKey("Managed Startup")
                        My.Computer.Registry.Users.OpenSubKey(user & "\Software", True).DeleteSubKey("Startup Manager")
                    Catch
                    End Try
                Next

                'Replace all users startup folder
                Dim startnames() As String = System.IO.Directory.GetFiles("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\All Managed Startup")
                If Not startnames.Length = 0 Then
                    For i As Integer = 0 To startnames.Length - 1
                        System.IO.File.Move("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\All Managed Startup\" & startnames(i).Substring(73), _
                                            "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\" & startnames(i).Substring(73))
                    Next
                    System.IO.Directory.Delete("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\All Managed Startup\")
                End If

                'Replace individual users startup folder
                Dim startusers() As String = System.IO.Directory.GetDirectories("C:\Users")
                For Each user As String In startusers
                    Dim path As String = user & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Managed Startup\"
                    If System.IO.Directory.Exists(path) Then
                        Dim startaunames() As String = System.IO.Directory.GetFiles(path)
                        Dim name As String = ""
                        If Not startaunames.Length = 0 Then
                            For i As Integer = 0 To startaunames.Length - 1
                                name = startaunames(i).Substring(path.Length)
                                System.IO.File.Move(user & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Managed Startup\" & name, _
                                                    user & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\" & name)
                            Next
                        End If
                        IO.Directory.Delete(path)
                    End If
                Next

                Try
                    System.IO.Directory.Delete("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup Manager", True)
                Catch
                End Try

            Try
                Dim proc As New Process
                Dim procargs As New ProcessStartInfo("CMD.exe", "/c echo Finished uninstalling Startup Manager && Pause && rmdir /s /q " & """"c & "C:\Program Files\Startup Manager" & """"c)
                procargs.Verb = "runas"
                proc.StartInfo = procargs
                proc.Start()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            Catch ex As Exception
                Console.WriteLine("There were errors uninstalling Startup Manager")
                Console.WriteLine(""""c & ex.Message & """"c)
                Console.WriteLine("Press any key to exit")
                Console.ReadLine()
            End Try

    End Sub

End Module
